from rest_framework.views import APIView
from django.http import JsonResponse
from rest_framework import status
from .models import Category, CategorySerializer


class CategoryCreateView(APIView):
    def post(self, request, format=None):
        serializer = CategorySerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse({"code": "200", "data": serializer.data, "message": "创建成功", "ok": "true"},
                                status=status.HTTP_200_OK)
        return JsonResponse({"code": "400", "data": serializer.errors, "message": "创建失败", "ok": "false"},
                            status=status.HTTP_400_BAD_REQUEST)


class CategoryListView(APIView):
    def get(self, request, format=None):
        categories = Category.objects.all()
        serializer = CategorySerializer(categories, many=True)
        return JsonResponse({"code": "200", "data": serializer.data, "message": "查询成功", "ok": "true"},
                            status=status.HTTP_200_OK)


class BlogCategoryListView(APIView):
    def get(self, request, format=None):
        categories = Category.objects.all()
        serializer = CategorySerializer(categories, many=True)
        return JsonResponse({"code": "200", "data": serializer.data, "message": "查询成功", "ok": "true"},
                            status=status.HTTP_200_OK)


class CategoryDetailView(APIView):
    def get_object(self, pk):
        try:
            return Category.objects.get(pk=pk)
        except Category.DoesNotExist:
            return None

    def get(self, request, format=None):
        pk = request.data.get('id')
        if not pk:
            return JsonResponse({"code": "400", "data": None, "message": "缺少分类ID", "ok": "false"},
                                status=status.HTTP_400_BAD_REQUEST)
        category = self.get_object(pk)
        if category is None:
            return JsonResponse({"code": "404", "data": None, "message": "分类不存在", "ok": "false"},
                                status=status.HTTP_404_NOT_FOUND)
        serializer = CategorySerializer(category)
        return JsonResponse({"code": "200", "data": serializer.data, "message": "查询成功", "ok": "true"},
                            status=status.HTTP_200_OK)

    def put(self, request, format=None):
        pk = request.data.get('id')
        if not pk:
            return JsonResponse({"code": "400", "data": None, "message": "缺少分类ID", "ok": "false"},
                                status=status.HTTP_400_BAD_REQUEST)
        category = self.get_object(pk)
        if category is None:
            return JsonResponse({"code": "404", "data": None, "message": "分类不存在", "ok": "false"},
                                status=status.HTTP_404_NOT_FOUND)
        serializer = CategorySerializer(category, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse({"code": "200", "data": serializer.data, "message": "更新成功", "ok": "true"},
                                status=status.HTTP_200_OK)
        return JsonResponse({"code": "400", "data": serializer.errors, "message": "更新失败", "ok": "false"},
                            status=status.HTTP_400_BAD_REQUEST)

    def delete(self, request, pk, format=None):
        category = self.get_object(pk)
        if category is None:
            return JsonResponse({"code": "404", "data": None, "message": "分类不存在", "ok": "false"},
                                status=status.HTTP_404_NOT_FOUND)
        category.delete()
        return JsonResponse({"code": "200", "data": None, "message": "删除成功", "ok": "true"},
                            status=status.HTTP_200_OK)
